<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <jsp:include page="../inc/js.jsp"/>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>特别分组用户管理</title>
    <jsp:include page="../inc/css.jsp"/>
</head>
<body class="hold-transition sidebar-mini">
<div class="wrapper">
    <jsp:include page="../inc/menu.jsp"/>

    <div class="content-wrapper">
        <section class="content">
            <div class="row">
                <div class="col-12">
                    <div class="card" style="margin-top: 10px">
                        <div class="card-header">
                            <h4>${specialGroup.content}</h4>

                            <div class="form-inline">
                                <div class="form-group">
                                    <input id="searchKeyWord" placeholder="关键词查询" class="form-control"/>
                                </div>

                                <button type="button" title="查询" class="btn btn-info tableTopBtn" onclick="_specialGroupChildUserList.fn.reloadData()"> 查询</button>
                                <button type="button" title="重置搜索条件并查询" class="btn btn-success tableTopBtn" onclick="_specialGroupChildUserList.fn.resetAndReloadData()"> 重置</button>
                                <button type="button" title="返回特别分组列表" class="btn btn-warning tableTopBtn" onclick="_specialGroupChildUserList.fn.goBack()">返回</button>
                                <button type="button" title="添加用户" class="btn btn-primary tableTopBtn" onclick="_specialGroupChildUserList.fn.addInfo()">添加用户</button>
                            </div>
                        </div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-hover" id="dataTables" style="width: 99%">
                                    <colgroup>
                                        <col class="gradeA even"/>
                                        <col class="gradeA odd"/>
                                        <col class="gradeA even"/>
                                        <col class="gradeA odd"/>
                                        <col class="gradeA even"/>
                                        <col class="gradeA odd"/>
                                    </colgroup>
                                    <thead>
                                    <tr>
                                        <th>微信昵称</th>
                                        <th>用户名</th>
                                        <th>备注</th>
                                        <th>联系方式</th>
                                        <th>头像</th>
                                        <th style="min-width: 60px">操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    <!-- /.card -->
                </div>
                <!-- /.col -->
            </div>
            <!-- /.row -->
        </section>
        <!-- /.content -->
    </div>
</div>
<!-- ./wrapper -->

<div class="modal inModal fade" id="specialGroupUserModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">特别公司用户关联详情</h4>
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            </div>
            <div class="modal-body">
                <form role="form" class="form-group">
                    <div class="form-inline" style="margin-bottom: 10px;">
                        <label class="col-3">用户：</label>

                        <div class="col-9">
                            <select id="userList" class="form-control" style="width: 95%">
                                <option value="">请选择用户</option>
                            </select>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="_specialGroupChildUserList.fn.saveInfo()">保存</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

<input id="hiddenSpecialGroupId" type="hidden" value="${specialGroup.id}"/>
<input id="hiddenCompanyId" type="hidden" value="${specialGroup.company.id}"/>

<!-- page script -->
<script type="text/javascript">
    let _specialGroupChildUserList = {
        v: {
            list: [],
            dTable: null
        },
        fn: {
            init: function () {
                _specialGroupChildUserList.fn.getUserList();

                _specialGroupChildUserList.fn.dataTableInit();
            },
            getUserList: function () {
                $.post('common/userList', {
                    companyId: $('#hiddenCompanyId').val()
                }, function (result) {
                    if (result.length > 0) {
                        let content = '';

                        $.each(result, function (i, item) {
                            content += '<option value="' + item.code + '">' + item.msg + '</option>';
                        });

                        $('#userList').append(content);
                    } else {
                        info(result.msg, "error");
                    }
                });
            },
            goBack: function () {
                window.history.go(-1);
            },
            reloadData: function () {
                _specialGroupChildUserList.v.list = [];
                _specialGroupChildUserList.v.dTable.DataTable().ajax.reload(null, false);
            },
            resetAndReloadData: function () {
                $('#searchKeyWord').val('');

                _specialGroupChildUserList.fn.reloadData();
            },
            dataTableInit: function () {
                _specialGroupChildUserList.v.dTable = $tutu.dataTable($('#dataTables'), {
                    "processing": true,
                    "serverSide": true,
                    "autoWidth": true,//自动计算宽度
                    "lengthChange": false,//是否允许公司改变表格每页显示的记录数
                    "searching": false,
                    "ordering": false,
                    "ajax": {
                        "url": "backend/specialGroup/child/user/list",
                        "type": "POST"
                    },
                    "language": $tutu.v.dataTableL,
                    "columns": [
                        {
                            "data": "user.nickName",
                            render: function (data) {
                                return formatStr(data, 10);
                            }
                        },
                        {
                            "data": "user.name",
                            render: function (data) {
                                return formatStr(data, 10);
                            }
                        },
                        {
                            "data": "user.description",
                            render: function (data) {
                                return formatStr(data, 20);
                            }
                        },
                        {"data": "user.mobile"},
                        {
                            "data": "user.avatar",
                            render: function (data) {
                                return formatPicUrl(data);
                            }
                        },
                        {"data": null}
                    ],
                    /*添加小插件*/
                    "columnDefs": [
                        {
                            "data": null,
                            "defaultContent": "<button type='button' title='移除用户' class='btn btn-danger btn-sm delete'><i class='fa fa-remove'></i></button>",
                            "targets": -1
                        }
                    ],
                    "createdRow": function (row, data, index) {
                        _specialGroupChildUserList.v.list.push(data);
                    },
                    rowCallback: function (row, data) {
                        $('td', row).last().find(".delete").click(function () {
                            _specialGroupChildUserList.fn.delInfo(data.id);
                        });
                    },
                    "fnServerParams": function (aoData) {
                        _specialGroupChildUserList.v.list = [];

                        aoData.specialGroupId = $('#hiddenSpecialGroupId').val();
                        aoData.keyWord = $('#searchKeyWord').val();
                    },
                    "fnDrawCallback": function (data) {
                        $tutu.uiForm(data);
                    }
                });
            },
            addInfo: function () {
                $("#specialGroupUserModal").modal("show");
            },
            saveInfo: function () {
                let userId = $('#userList').val();

                if (userId === '') {
                    info('请选择用户', "error");
                    return;
                }

                startLoading();

                $.post('backend/specialGroup/child/user/save', {
                    specialGroupId: $('#hiddenSpecialGroupId').val(),
                    userId: userId
                }, function (result) {
                    endLoading();

                    if (result.code === 0) {
                        info("信息已保存", "success");

                        $("#specialGroupUserModal").modal("hide");

                        _specialGroupChildUserList.fn.reloadData();
                    } else {
                        info(result.msg, "error");
                    }
                });
            },
            delInfo: function (id) {
                startLoading();

                $.post('backend/specialGroup/child/user/del', {
                    id: id
                }, function (result) {
                    endLoading();

                    if (result.code === 0) {
                        info("信息已删除", "success");

                        _specialGroupChildUserList.fn.reloadData();
                    } else {
                        info(result.msg, "error");
                    }
                });
            }
        }
    };

    $(document).ready(function () {
        _specialGroupChildUserList.fn.init();

        window.onload = function () {
            checkSelectMenu("specialGroupLi");

            loadMenuColor('specialGroupLi');
        }
    });
</script>
</body>
</html>